/* -----------------------  
 * CylinderChartDemo1.java  
 * -----------------------  
 * (C) Copyright 2005, by Object Refinery Limited.  
 *  
 */   
   
package com.kingre.et.chart;   
   
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Paint;
import java.util.List;
import java.util.Map;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.GradientPaintTransformType;
import org.jfree.ui.StandardGradientPaintTransformer;
import org.jfree.ui.TextAnchor;
   
/**  
 * A simple demonstration application showing how to create a cylinder   
 * chart using data from a {@link CategoryDataset}.  
 */   
public class CylinderChart{   
    private static final long serialVersionUID = -9076769238990972479L;

    /**  
     * A custom renderer that returns a different color for each item in a   
     * single series.  
     */   
    static class CustomCylinderRenderer extends CylinderRenderer {   
        private static final long serialVersionUID = 1244431100776394627L;
        /** The colors. */   
        private Paint[] colors;   
   
        /**  
         * Creates a new renderer.  
         *  
         * @param colors  the colors.  
         */   
        public CustomCylinderRenderer(Paint[] colors) {   
            this.colors = colors;   
        }   
   
        /**  
         * Returns the paint for an item.  Overrides the default behaviour   
         * inherited from AbstractSeriesRenderer.  
         *  
         * @param row  the series.  
         * @param column  the category.  
         *  
         * @return The item color.  
         */   
        public Paint getItemPaint(int row, int column) {   
            return this.colors[column % this.colors.length];   
        }   
    }   
   
    public static JFreeChart generateChart(String title, String xLabel,
            String yLabel, List<Map<String, String>> list){
        CategoryDataset dataset = createDataset(list);   
        JFreeChart chart = createChart(title, xLabel, yLabel, dataset);   
        return chart;
    }
     
    /**  
     * Creates a sample dataset.  
     *  
     * @return a sample dataset.  
     */   
    private static CategoryDataset createDataset(List<Map<String, String>> list) {   
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for(Map<String, String> map : list){
            dataset.addValue(Double.parseDouble(map.get("value")), "S1", map.get("vd"));
        }
        return dataset;      
    }   
       
    /**  
     * Creates a chart.  
     *   
     * @param dataset  the dataset.  
     *   
     * @return The chart.  
     */   
    private static JFreeChart createChart(String title, String xLabel,
            String yLabel, CategoryDataset dataset) {   
           
        JFreeChart chart = ChartFactory.createBarChart3D(   
            title,                    // chart title   
            xLabel,                   // domain axis label   
            yLabel,                   // range axis label   
            dataset,                  // data   
            PlotOrientation.VERTICAL, // orientation   
            false,                    // include legend   
            true,                     // tooltips   
            false                     // urls   
        );   
   
        CategoryPlot plot = (CategoryPlot) chart.getPlot();   
        CategoryAxis domainAxis = plot.getDomainAxis();
        NumberAxis numberaxis = (NumberAxis)plot.getRangeAxis();
        
        ValueAxis rangeAxis = plot.getRangeAxis();
        //设置最高的一个柱与图片顶端的距离
        rangeAxis.setUpperMargin(0.08);

        domainAxis.setLowerMargin(0.01);
        domainAxis.setUpperMargin(0.01);//设置距离图片右端距离此时为百分之10
        
        Paint[] colors = createPaint();   
        CustomCylinderRenderer barRenderer = new CustomCylinderRenderer(colors);   
        barRenderer.setItemLabelPaint(Color.black);
        barRenderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        barRenderer.setItemLabelFont(new Font("黑体",Font.PLAIN,14));
        barRenderer.setItemLabelPaint(Color.black);
        barRenderer.setItemLabelsVisible(true);
        ItemLabelPosition itemlabelposition = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER);   
        barRenderer.setPositiveItemLabelPosition(itemlabelposition);   
        
        chart.getTitle().setFont(new Font("宋体", Font.PLAIN, 16));
        chart.setBackgroundPaint(Color.WHITE);
        /*------设置X轴坐标上的文字-----------*/
        domainAxis.setTickLabelFont(new Font("黑体", Font.PLAIN, 11));
        /*------设置X轴的标题文字------------*/
        domainAxis.setLabelFont(new Font("黑体", Font.PLAIN, 14));
        /*------设置Y轴坐标上的文字-----------*/
        numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
        /*------设置Y轴的标题文字------------*/
        numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 14));
        
        numberaxis.setTickUnit(new NumberTickUnit(10));
        numberaxis.setRange(0D, 90D);

        barRenderer.setGradientPaintTransformer(   
                new StandardGradientPaintTransformer(   
                        GradientPaintTransformType.CENTER_HORIZONTAL));   
        barRenderer.setOutlinePaint(Color.gray);   
        barRenderer.setOutlineStroke(new BasicStroke(0.3f));   
        barRenderer.setMaximumBarWidth(0.06);
        plot.setRenderer(barRenderer);   
        return chart;   
   
    }   
       
    /**  
     * Returns an array of paint objects that will be used for the bar colors.  
     *   
     * @return An array of paint objects.  
     */   
    private static Paint[] createPaint() {   
        Paint[] colors = new Paint[9];   
        colors[0] = new GradientPaint(0f, 0f, Color.white, 0f, 0f, Color.red);   
        colors[1] = new GradientPaint(0f, 0f, Color.white, 0f, 0f, Color.green);   
        colors[2] = new GradientPaint(0f, 0f, Color.white, 0f, 0f, Color.blue);   
        colors[3] = new GradientPaint(0f, 0f, Color.white, 0f, 0f, Color.orange);   
        colors[4] = new GradientPaint(0f, 0f, Color.white, 0f, 0f, Color.magenta);   
        colors[5] = new GradientPaint(0f, 0f, Color.white, 0f, 0f, Color.cyan);   
        colors[6] = new GradientPaint(0f, 0f, Color.white, 0f, 0f, Color.gray);   
        colors[7] = new GradientPaint(0f, 0f, Color.white, 0f, 0f, Color.pink);   
        colors[8] = new GradientPaint(0f, 0f, Color.white, 0f, 0f, Color.yellow);   
        return colors;   
    }   
       
}   